import { getCompanyID, getPosIDs, getUserDepID, getUserID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { disposeArea } from '../common';
import { getRequestIp } from '../../Acommon';
// let baseUrl = getBaseUrl();
// ==============================振豪IP===============================
// const baseUrl = 'http://192.168.1.33:31019'
// let baseUrl = 'http://192.168.153.192:31019';
// ==============================振豪IP===============================
// let baseUrl = '/materilManage';

/**
 * 添加物料
 * */
export const addMaterialServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };

      /***处理省市县**/
      bodyParams = disposeArea(bodyParams);

      let reqUrl = `${getRequestIp()}/materialController/add`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );

  return {
    addMaterialRun: request?.run,
    addMaterialLoading: request?.loading,
  };
};

/**
 * 编辑物料
 * */
export const editMaterialServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };

      /***处理省市县**/
      bodyParams = disposeArea(bodyParams);

      let reqUrl = `${getRequestIp()}/materialController/edit`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );

  return {
    editMaterialRun: request?.run,
    editMaterialLoading: request?.loading,
  };
};

/**
 * 物料详情
 * */
export const detailMaterialServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${getRequestIp()}/materialController/details/${bodyParams?.id}`;
      return requestFunComponFiex('GET', reqUrl, bodyParams?.id);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    detailMaterialRun: request?.run,
    detailMaterialLoading: request?.loading,
  };
};

/**
 * 作废
 * */
export const cancelMaterialServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        cancelerID: getUserID(),
        cancelPositionID: getPosIDs(),
        cancelDepartmentID: getUserDepID(),
        cancelCompanyID: getCompanyID(),
      };
      let reqUrl = `${getRequestIp()}/materialController/cancel`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );

  return {
    cancelMaterialRun: request?.run,
    cancelMaterialLoading: request?.loading,
  };
};

/**
 * 发布
 * */
export const publishMaterialServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        signerID: getUserID(),
        signPositionID: getPosIDs(),
        signDepartmentID: getUserDepID(),
        signCompanyID: getCompanyID(),
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        signType: 0,
      };
      bodyParams = disposeArea(bodyParams);
      let reqUrl = `${getRequestIp()}/materialController/publish`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );

  return {
    publishMaterialRun: request?.run,
    publishMaterialLoading: request?.loading,
  };
};

/**
 * 申请
 * */
export const applyMaterialServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        signType: 1,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };
      bodyParams = disposeArea(bodyParams);
      let reqUrl = `${getRequestIp()}/materialController/applyMaterial`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );

  return {
    applyMaterialRun: request?.run,
    applyMaterialLoading: request?.loading,
  };
};

/**
 * 审核
 * */
export const auditMaterialServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        checkerID: getUserID(),
        checkPositionID: getPosIDs(),
        checkDepartmentID: getUserDepID(),
        checkCompanyID: getCompanyID(),
      };
      bodyParams = disposeArea(bodyParams);
      let reqUrl = `${getRequestIp()}/materialController/audit`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );

  return {
    auditMaterialRun: request?.run,
    auditMaterialLoading: request?.loading,
  };
};

/**
 * 批量添加物料
 * */
export const batchAddMaterialServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        signerID: getUserID(),
        signPositionID: getPosIDs(),
        signDepartmentID: getUserDepID(),
        signCompanyID: getCompanyID(),
      };

      let reqUrl = `${getRequestIp()}/materialController/batchAdd`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );

  return {
    batchAddMaterialRun: request?.run,
    batchAddMaterialLoading: request?.loading,
    batchAddMaterialData: request?.data?.obj?.mmaterialList,
  };
};
